package com.micro.ai.agents.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.agents.entity.Agent;
import com.micro.ai.agents.service.AgentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 智能体控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/agents")
@Tag(name = "智能体管理", description = "智能体的增删改查、状态管理、版本发布等功能")
public class AgentController {

    @Autowired
    private AgentService agentService;

    @PostMapping
    @Operation(summary = "创建智能体", description = "创建新的智能体")
    public ApiResponse<Agent> create(@Valid @RequestBody Agent agent) {
        log.info("创建智能体请求: tenantId={}, name={}", agent.getTenantId(), agent.getName());
        Agent result = agentService.create(agent);
        return ApiResponse.success("创建成功", result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新智能体", description = "更新智能体信息")
    public ApiResponse<Agent> update(
            @Parameter(description = "智能体ID", required = true) @PathVariable String id,
            @Valid @RequestBody Agent agent) {
        log.info("更新智能体请求: agentId={}", id);
        Agent result = agentService.update(id, agent);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除智能体", description = "删除智能体")
    public ApiResponse<Void> delete(
            @Parameter(description = "智能体ID", required = true) @PathVariable String id) {
        log.info("删除智能体请求: agentId={}", id);
        agentService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取智能体详情", description = "根据ID获取智能体详细信息")
    public ApiResponse<Agent> getById(
            @Parameter(description = "智能体ID", required = true) @PathVariable String id) {
        Agent agent = agentService.getById(id);
        if (agent == null) {
            return ApiResponse.error("A0002", "智能体不存在");
        }
        return ApiResponse.success(agent);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询智能体", description = "支持多条件查询智能体列表")
    public ApiResponse<Page<Agent>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "关键词") @RequestParam(value = "keyword", required = false) String keyword,
            @Parameter(description = "状态") @RequestParam(value = "status", required = false) String status) {
        Page<Agent> page = agentService.pageQuery(pageNum, pageSize, tenantId, keyword, status);
        return ApiResponse.success(page);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新智能体状态", description = "启用、停用或标记为训练中")
    public ApiResponse<Void> updateStatus(
            @Parameter(description = "智能体ID", required = true) @PathVariable String id,
            @Parameter(description = "状态", required = true) @RequestParam("status") String status) {
        log.info("更新智能体状态请求: agentId={}, status={}", id, status);
        agentService.updateStatus(id, status);
        return ApiResponse.successVoid("更新成功");
    }

    @PutMapping("/{id}/publish-version")
    @Operation(summary = "发布新版本", description = "为智能体发布新版本")
    public ApiResponse<Agent> publishVersion(
            @Parameter(description = "智能体ID", required = true) @PathVariable String id,
            @Parameter(description = "版本号", required = true) @RequestParam("version") String version,
            @Parameter(description = "变更日志") @RequestParam(value = "changeLog", required = false) String changeLog) {
        log.info("发布智能体版本请求: agentId={}, version={}", id, version);
        Agent result = agentService.publishVersion(id, version, changeLog);
        return ApiResponse.success("发布成功", result);
    }
}

